namespace CIS
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class CityManage : Page
    {
        protected DropDownList AreaDdl;
        protected DataList CityDL;
        protected Image iRegionManage;
        protected Label lbArea;
        protected Label lbProvince;
        protected UserPrincipal Principal = null;
        protected DropDownList ProvinceDdl;

        private void AreaDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ProvinceDdl.DataSource = ManageProvince.GetProvince(new Guid(this.AreaDdl.SelectedValue));
            this.ProvinceDdl.DataTextField = "RegionName";
            this.ProvinceDdl.DataValueField = "RegionId";
            this.ProvinceDdl.DataBind();
        }

        private void CityDL_CancelCommand(object source, DataListCommandEventArgs e)
        {
            this.CityDL.EditItemIndex = -1;
            this.CityDLDataBind();
        }

        private void CityDL_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            ManageCity.RemoveCityViaId((Guid) this.CityDL.DataKeys[e.Item.ItemIndex]);
            this.CityDL.EditItemIndex = -1;
            this.CityDLDataBind();
        }

        private void CityDL_EditCommand(object source, DataListCommandEventArgs e)
        {
            this.CityDL.EditItemIndex = e.Item.ItemIndex;
            this.CityDLDataBind();
        }

        private void CityDL_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string str;
            string cmd = e.CommandName;
            if (((str = cmd) != null) && (string.IsInterned(str) == "new"))
            {
                TextBox NameTbx = (TextBox) e.Item.FindControl("txtNewName");
                TextBox SCNameTbx = (TextBox) e.Item.FindControl("txtNewSCName");
                if ((NameTbx != null) && (SCNameTbx != null))
                {
                    string CityName = NameTbx.Text.Trim();
                    string CitySCName = SCNameTbx.Text.Trim();
                    if ((CityName != "") && (CitySCName != ""))
                    {
                        if (ManageCity.IsExistSameCityName(CityName, new Guid(this.AreaDdl.SelectedValue)))
                        {
                            base.Response.Write("<script> alert('已存在该省') </script>");
                            this.CityDLDataBind();
                        }
                        else
                        {
                            ManageCity.InsertNewCity(CityName, CitySCName, new Guid(this.ProvinceDdl.SelectedItem.Value), new Guid(this.AreaDdl.SelectedValue));
                            this.CityDLDataBind();
                        }
                    }
                }
            }
        }

        private void CityDL_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            TextBox txtName = e.Item.FindControl("txtName") as TextBox;
            TextBox txtSCName = e.Item.FindControl("txtSCName") as TextBox;
            if ((txtName != null) && (txtSCName != null))
            {
                string CityName = txtName.Text.Trim();
                string CitySCName = txtSCName.Text.Trim();
                if (!(CitySCName == "") && !(CityName == ""))
                {
                    ManageCity.UpdateCity(CityName, CitySCName, (Guid) this.CityDL.DataKeys[e.Item.ItemIndex]);
                    this.CityDL.EditItemIndex = -1;
                    this.CityDLDataBind();
                }
            }
        }

        private void CityDLDataBind()
        {
            if (this.ProvinceDdl.SelectedValue == "00000000-0000-0000-0000-000000000000")
            {
                this.CityDL.DataSource = ManageCity.GetCity(new Guid(this.AreaDdl.SelectedValue));
            }
            else
            {
                this.CityDL.DataSource = ManageCity.GetCityViaProvinceId(new Guid(this.ProvinceDdl.SelectedValue));
            }
            this.CityDL.DataBind();
        }

        protected string GetCityProcinceName(object strCityName, object provinceId)
        {
            return (((string) strCityName) + " - " + ManageProvince.GetProvinceViaId((Guid) provinceId).ProvinceName);
        }

        protected string GetStrInSetFile(string str)
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            return ManageSystemConfig.GetSystemSetLanguage("CityManage", str, language, path);
        }

        private void InitializeComponent()
        {
            this.AreaDdl.SelectedIndexChanged += new EventHandler(this.AreaDdl_SelectedIndexChanged);
            this.ProvinceDdl.SelectedIndexChanged += new EventHandler(this.ProvinceDdl_SelectedIndexChanged);
            this.CityDL.ItemCommand += new DataListCommandEventHandler(this.CityDL_ItemCommand);
            this.CityDL.CancelCommand += new DataListCommandEventHandler(this.CityDL_CancelCommand);
            this.CityDL.EditCommand += new DataListCommandEventHandler(this.CityDL_EditCommand);
            this.CityDL.UpdateCommand += new DataListCommandEventHandler(this.CityDL_UpdateCommand);
            this.CityDL.DeleteCommand += new DataListCommandEventHandler(this.CityDL_DeleteCommand);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
            for (int i = 0; i < area.Length; i++)
            {
                ListItem tmpListItem = new ListItem();
                tmpListItem.Text = area[i].AreaName;
                tmpListItem.Value = area[i].AreaId.ToString();
                this.AreaDdl.Items.Add(tmpListItem);
            }
            this.ProvinceDdl.DataSource = ManageProvince.GetProvince(new Guid(this.AreaDdl.SelectedValue));
            this.ProvinceDdl.DataTextField = "RegionName";
            this.ProvinceDdl.DataValueField = "RegionId";
            this.ProvinceDdl.DataBind();
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!base.IsPostBack)
            {
                this.SetFrom();
                this.InitPage();
                this.SetFromImage();
                this.CityDLDataBind();
            }
        }

        private void ProvinceDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.CityDLDataBind();
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.lbProvince.Text = ManageSystemConfig.GetSystemSetLanguage("CityManage", "lbProvinceOverSea", language, path);
            }
            else
            {
                this.lbProvince.Text = ManageSystemConfig.GetSystemSetLanguage("CityManage", "lbProvince", language, path);
            }
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iRegionManage.ImageUrl = "images/title_RegionManage.jpg";
                    break;

                case "en":
                    this.iRegionManage.ImageUrl = "images/title_RegionManageEn.jpg";
                    break;
            }
        }
    }
}

